package com.itcast.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itcast.mapper.GoodsMapper;
import com.itcast.pojo.Goods;
import com.itcast.pojo.GoodsConditionData;
import com.itcast.service.Goodsservice;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@Service("goodsservice")
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements Goodsservice {

    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    /**
     * 热销产品
     * @return
     */
    @Override
    public List<Goods> selectproductsrank(String search) {
        return goodsMapper.selectproductsrank(search);
    }

    /**
     * 新品推荐
     * @return
     */
    @Override
    public List<Goods> newGoods(String search) {

        return goodsMapper.newGoods(search);
    }

    /**
     * 好品推荐
     * @return
     */
    @Override
    public List<Goods> goodProductsRank(String search) {
        return goodsMapper.goodProductsRank(search);
    }

    @Override
    public Goods getByIdGoods(Integer id) {
        return goodsMapper.getByIdGoods(id);
    }

    @Override
    public List<Goods> getByItemGoods(String item) {
        return goodsMapper.getByItemGoods(item);
    }

    @Override
    public List<Goods> getByPrice() {
        return goodsMapper.getByPrice();
    }

    @Override
    public List<Goods> getByView() {
        return goodsMapper.getByView();
    }



    @Override
    public PageInfo<Goods> getGoodsWithPage1(Integer pageIndex, Integer pageSize,Goods goods) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Goods>users=goodsMapper.findGoodsByCondition(goods);
        return new PageInfo<>(users, 3);
    }

    /**
     * 数据查询条件处理
     * @param conditionData
     */
    public GoodsConditionData getConditionData(Map<String,String> conditionData){
        GoodsConditionData goodsConditionData =new GoodsConditionData();
        String typeName = conditionData.get("typeName"); // 商品类型名称
        String sortWay = conditionData.get("sortWay");//排序方式
        String search =conditionData.get("search");//查询条件
        // 排序方式
        Integer startPage =1;
        Integer pageNumber=6;
        if (!conditionData.get("startPage").trim().equals("")){
            startPage = Integer.valueOf(conditionData.get("startPage").trim()); // 第几页
        }
        if (!conditionData.get("pageNumber").trim().equals("")){
            pageNumber = Integer.valueOf(conditionData.get("pageNumber").trim()); // 一页几条
        }
        PageHelper.startPage(startPage,pageNumber);
        goodsConditionData.setTypeName(typeName);
        goodsConditionData.setSearch(search);
        goodsConditionData.setSortWay(sortWay);
        return goodsConditionData;
    }

    /*
           PageHelper.startPage(pageNum,pageSize);//这行是重点，表示从pageNum页开始，每页pageSize条数据
           List<Tools> list = toolsMapper.findAll();
           PageInfo<Tools> pageInfo = new PageInfo<Tools>(list);
           return ServerResponse.createBySuccess("查询成功",pageInfo);
        */

    /**
     * 新品带条件查询
     * @param conditionData
     * @return
     */
    @Override
    public PageInfo<Goods> getGoodsByConditionAndPage(Map<String,String> conditionData) {
        System.out.println(conditionData);
        try {
           GoodsConditionData goodsConditionData= getConditionData(conditionData);
            List<Goods> goodsList = goodsMapper.getGoodsByCondition(goodsConditionData.getTypeName(),goodsConditionData.getSortWay(),goodsConditionData.getSearch());
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);
            return pageInfo;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 全部宝贝带条件查询
     * @param conditionData
     * @return
     */
    @Override
    public PageInfo<Goods> getAllGoodsByConditionAndPage(Map<String, String> conditionData) {
        try {
            GoodsConditionData goodsConditionData= getConditionData(conditionData);
            List<Goods> goodsList = goodsMapper.getAllGoodsByConditionAndPage(goodsConditionData.getTypeName(),goodsConditionData.getSortWay(),goodsConditionData.getSearch());
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);
            return pageInfo;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 热销品带条件查询
     * @param conditionData
     * @return
     */
    @Override
    public PageInfo<Goods> getGoodsHotByConditionAndPage(Map<String, String> conditionData) {
        try {
            GoodsConditionData goodsConditionData= getConditionData(conditionData);
            List<Goods> goodsList = goodsMapper.getGoodsHotByConditionAndPage(goodsConditionData.getTypeName(),goodsConditionData.getSortWay(),goodsConditionData.getSearch());
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);
            return pageInfo;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 猜你喜欢
     * @param
     * @return
     */
    @Override
    public List<Goods> selectproductsliketest(ArrayList<Integer> gid) {
        return goodsMapper.selectproductsliketest(gid);
    }

    @Override
    public List<Goods> selectproductslike3() {
        return goodsMapper.selectproductslike3();
    }


    @Override
    public List<Goods> selectproductslike2() {
        return goodsMapper.selectproductslike2();
    }


    @Override
    public List<Goods> getByUserId(String userId) {
        return goodsMapper.getByUserId(userId);
    }

    /**
     * 带条件收藏
     * @param conditionData
     * @return
     */
    @Override
    public PageInfo<Goods> getCollectByUser(Map<String, String> conditionData) {
        try {
            String search =conditionData.get("search");//查询条件
            Integer userId =Integer.valueOf(conditionData.get("userId"));
            // 排序方式
            Integer startPage =1;
            Integer pageNumber=12;
            if (!conditionData.get("startPage").trim().equals("")){
                startPage = Integer.valueOf(conditionData.get("startPage").trim()); // 第几页
            }
            PageHelper.startPage(startPage,pageNumber);
            List<Goods> goodsList = goodsMapper.getCollectByUser(search,userId);
            PageInfo<Goods> pageInfo = new PageInfo<Goods>(goodsList);
            return pageInfo;
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
